using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

using TwitterClient;

namespace SigTweet
{
	[MonoTouch.Foundation.Register("TwitterTableViewController")]
	public partial class TwitterTableViewController : UITableViewController
	{

		private static NSString kCellIdentifier = new NSString ("tweetCell");

		//
		// Constructor invoked from the NIB loader
		//
		public TwitterTableViewController (IntPtr p) : base(p)
		{
		}


		//
		// The source for our TableView
		//
		class TableSource : UITableViewSource
		{

			public TableSource (TwitterTableViewController tableController)
			{
				_tableController = tableController;
				_cellControllers = new Dictionary<int, TwitterTableCellController> ();
			}

			public override int RowsInSection (UITableView tableView, int section)
			{
				return _tableController._tweets.Count;
			}

			public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
			{
				var cell = tableView.DequeueReusableCell (kCellIdentifier);
				var cellController = default(TwitterTableCellController);
				
				if (cell == null) {
					cellController = new TwitterTableCellController ();
					
					NSBundle.MainBundle.LoadNib ("TwitterTableCellController", cellController, null);
					
					cell = cellController.Cell;
					
					cell.Tag = (++_vCellIndex);
					_cellControllers.Add (cell.Tag, cellController);
				} else
					cellController = _cellControllers[cell.Tag];
				
				cellController.CurrentTweet = _tableController._tweets[indexPath.Row];
				
				return cell;
			}

			public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
			{
				if (_detailController == null)
					_detailController = new TweetDetailViewController ();
				
				_detailController.CurrentTweet = _tableController._tweets[indexPath.Row];
				
				_tableController.NavigationController.PushViewController (_detailController, true);
			}

			private int _vCellIndex;

			private TweetDetailViewController _detailController;
			private TwitterTableViewController _tableController;

			private Dictionary<int, TwitterTableCellController> _cellControllers;
		}

		public override bool CanBecomeFirstResponder {
			get { return true; }
		}

		public override void ViewDidAppear (bool animated)
		{
			base.ViewDidAppear (animated);
			BecomeFirstResponder ();
		}

		public override void ViewWillDisappear (bool animated)
		{
			ResignFirstResponder ();
			base.ViewWillDisappear (animated);
		}

		public override void MotionEnded (UIEventSubtype motion, UIEvent evt)
		{
			if (motion == UIEventSubtype.MotionShake) {
				LoadTweets ();
				TableView.ReloadData ();
			}
		}

		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			Title = String.Format ("@{0}'s Friends", Configuration.AccountUserName);
			
			LoadTweets ();
			
			TableView.Source = new TableSource (this);
		}

		private TwitterProxy Proxy {
			get {
				
				if (_twitterProxy == null) {
					_twitterProxy = new TwitterProxy ();
					
					_twitterProxy.AccountUserName = Configuration.AccountUserName;
					_twitterProxy.AccountPassword = Configuration.AccountPassword;
				}
				
				return _twitterProxy;
			}
		}

		private void LoadTweets ()
		{
			UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
			
			_tweets = Proxy.RetrieveFriendUpdates (20);
			
			UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
		}

		private TwitterProxy _twitterProxy;
		private List<Tweet> _tweets;
	}
}
